﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SMS
{
    public partial class uThietlapcauhinhtinnhan : UserControl
    {
        fLogin main;
        public uThietlapcauhinhtinnhan(fLogin main1)
        {
            InitializeComponent();
            main = main1;
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void bntHuy_Click(object sender, EventArgs e)
        {
            cboLoaiTN_SelectedIndexChanged(null, null);
        }

        private void bntSave_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(main.ConnectDB);
            SqlCommand command = new SqlCommand();
            command.Connection = conn;
            command.CommandText = "UPDATE LOAITINNHAN SET NOIDUNG = @NOIDUNG, NGAYBATDAUGUI = @NGAYBATDAUGUI, NGAYKETTHUCGUI = @NGAYKETTHUCGUI, HANBD = @HANBAODUONG "
                    + "WHERE LOAITN = @LOAITN";
            command.Parameters.Add(new SqlParameter("@NOIDUNG", txtNoiDung.Text));
            command.Parameters.Add(new SqlParameter("@NGAYBATDAUGUI", int.Parse(txtNgayBatDau.Text)));
            command.Parameters.Add(new SqlParameter("@NGAYKETTHUCGUI", int.Parse(txtNgayKetThuc.Text)));
            command.Parameters.Add(new SqlParameter("@HANBAODUONG", int.Parse(txtHanCuoiBd.Text)));
            command.Parameters.Add(new SqlParameter("@LOAITN", cboLoaiTN.Text));
            try
            {
                conn.Open();
                command.ExecuteNonQuery();
                MessageBox.Show("Cấu hình thành công !");
            }
            catch (SqlException ex)
            {
                MessageBox.Show("Cấu hình thất bại !");
            }

            finally
            {
                command.Dispose();
                conn.Close();
            }

        }

        private void uThietlapcauhinhtinnhan_Load(object sender, EventArgs e)
        {
            cboLoaiTN.DataSource = DSLoaiTN();

        }

        private string[] DSLoaiTN()
        {
            string[] DanhSach = new string[100];
            int i = 0;
            SqlConnection conn = new SqlConnection(main.ConnectDB);
            SqlCommand command = new SqlCommand();
            command.Connection = conn;
            command.CommandText = "SELECT LOAITN FROM LOAITINNHAN";

            try
            {
                conn.Open();
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    DanhSach[i] = reader[0].ToString();
                    i++;
                }


            }
            catch (SqlException ex)
            {
                string error = ex.Message;
            }
            finally
            {
                command.Dispose();
                conn.Close();
            }

            string[] result = PerfectArray(DanhSach);
            return result;
        }

        protected string[] PerfectArray(string[] array)
        {
            int count = 0;
            for (int i = 0; i < array.Length; i++)
            {
                if (array[i] != null)
                {
                    count++;
                }
            }
            string[] PerfectArray = new string[count];
            for (int i = 0; i < array.Length; i++)
            {
                if (array[i] != null)
                {
                    PerfectArray[i] = array[i];
                }
            }
            return PerfectArray;
        }

        private void cboLoaiTN_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(main.ConnectDB);
            SqlCommand command = new SqlCommand();
            command.Connection = conn;
            command.CommandText = "SELECT NGAYBATDAUGUI, NGAYKETTHUCGUI, NOIDUNG, HANBD FROM LOAITINNHAN "
                + "WHERE LOAITN = @LOAI";
            command.Parameters.Add(new SqlParameter("@LOAI", cboLoaiTN.Text));

            try
            {
                conn.Open();
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    txtNgayBatDau.Text = reader[0].ToString();
                    txtNgayKetThuc.Text = reader[1].ToString();
                    txtNoiDung.Text = reader[2].ToString();
                    txtHanCuoiBd.Text = reader[3].ToString();
                    txtLan.Text = cboLoaiTN.Text;
                    lblSoKiTu.Text = txtNoiDung.TextLength.ToString();
                }


            }
            catch (SqlException ex)
            {
                string error = ex.Message;
            }
            finally
            {
                command.Dispose();
                conn.Close();
            }

        }

        private void txtNgayBatDau_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtNgayBatDau_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsLetterOrDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && !Char.IsWhiteSpace(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtNoiDung_TextChanged(object sender, EventArgs e)
        {
            lblSoKiTu.Text = "" + txtNoiDung.TextLength;
        }
    }
}
